<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
	<!-- <style type="text/css"></style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="inquiryQuotationForm" th:action="@{/inquiryQuotation/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                               <label>汇总号：</label>
                               <input id="inquiryTotalCode" name="inquiryTotalCode" type="text" />
                        </li>
                        <li class="col-sm-3">
                              <label>物料编码：</label>
                              <input id="materialCode" name="materialCode" type="text" />
                        </li>
                        <li class="col-sm-3">
	                            <label>询价报价单号：</label>
	                            <input id="inquiryCode" name="inquiryCode" type="text" />
                        </li>
                        <li class="col-sm-3">
                            <label >状态：</label>
                            <select id="state" name="state">
                                <option></option>
                                <option value="-1">未报价</option>
                                <option value="0">草稿</option>
                                <option value="1">已提交</option>
                            </select>
                        </li>
                        <li class="col-sm-3">
                            <button id="inquiryQuotationSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
                            <button id="inquiryQuotationSearch" type="button" class="btn btn-primary btn-rounded btn-sm" style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索</button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        
        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="inquiryQuotationToolBar" role="group">
                <!--  <a class="btn btn-outline btn-success btn-rounded" onclick="inquiryQuotationadd()">
                     <i class="fa fa-plus"></i> 新增
                 </a>-->
                 <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="inquiryQuotationedit()">
                     <i class="fa fa-edit"></i> 编辑
                 </a> -->
                <!--   <a class="btn btn-outline btn-success btn-rounded" onclick="submit()">
                     <i class="fa fa-edit"></i> 提交
                 </a>-->
                <a class="btn btn-outline btn-success btn-rounded" data-toggle='modal' onclick="show()">
                    <i class="fa fa-envelope"></i> 发送邮件
                </a>
            </div>
            <div style="height:1000px; overflow:auto;">
            	<table id="inquiryQuotation" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div id="myMailModal" style="display: none">
    <div class="modal-body" >
        <div class="form-group">
            <label >收件人</label><label style="color: red">(多个收件人以英文字符 ; 分开)</label>
            <textarea  id="email" class="form-control" rows="5"></textarea>
        </div>
        <br/>
        <div class="form-group">
            <label >内容</label>
            <textarea id="remark" class="form-control" rows="5"></textarea>
        </div>
        <div class="form-group">
            <label >供应商</label>
            <textarea id="vendorName" class="form-control" rows="5"></textarea>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal"  onclick="closeDialog()">关闭</button>
        <button type="button" class="btn btn-primary" data-dissmiss="modal" onclick="sendmail()">发送</button>
    </div>
</div><!-- /.modal-dialog -->

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document" style="width:70%">
        <div class="modal-content" style="height:700px">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">模态框（Modal）标题</h4>
            </div>
                <div class="modal-body" style="height:90%;width:85%;overflow:auto">
			       	 <table class="table table-bordered text-nowrap" id="mail">
						   <thead>
								<tr>
								    <th>报价单号</th>
								    <th>邮箱</th>
								    <th>时间</th>
								</tr>
							</thead>
							<tbody>
							 <tr th:each="parityEmail : ${parityEmaillist}">
							 <td style="display:none"><input name="parityEmailId" type="hidden" th:value="${parityEmail.parityEmailId}" /></td>
						     <td width='110px'><input style='width:95%' type='text' name='parityEmailInquiryCode' th:value="${parityEmail.parityEmailInquiryCode}"/></td>
						     <td width='110px'><input style='width:95%' type='text' name='parityEmail' th:value="${parityEmail.parityEmail}"/></td>
						     <td width='110px'><input style='width:95%' type='text' name='data' th:value="${parityEmail.data}"/></td>
							 </tr>
							</tbody>
					</table>
                </div>
            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>&lt;!&ndash; /.modal-content &ndash;&gt;
    </div><!-- /.modal-dialog -->
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
$(function() {
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	$("#email").keydown(function(event) {
		if (event.keyCode == "13") {
		e = $(this).val().replace(/[\r\n]/g,"");
		arr=e.split(";");
		for(var i=0;i<arr.length;i++){
			alert(arr[i]);
			if(!myreg.test(arr[i])){
	            alert('提示\n\n请输入有效的E_mail！');
	            //myreg.focus();
	            return false;
			}
		}
		$(this).val(e + ';');
		}
	});
	
	$("#remarks").keydown(function(event) {
		if (event.keyCode == "13") {
		e = $(this).val();
		$(this).val(e + ',');
		}
	});
	
	$("#vendorNameid").keydown(function(event) {
		if (event.keyCode == "13") {
		e = $(this).val();
		$(this).val(e + ';');
		}
	});
});

</script>
<script type="text/javascript">
    //面向对象
    var searcherBS= Searcher.createBS('#inquiryQuotation',
        [
            {checkbox: true},
            {title: "询价报价单号", field:"inquiryCode"},
            {title: "状态 ",field:"state",
            	formatter : function (value, row, index) {
            		if(value == '0') {
            			return "草稿";
            		} else if(value == '1') {
            			return "已提交";
            		} else if(value == '-1'){
            			return "未报价";
            		}
            	}
            },
            {title: "汇总号",field:"inquiryTotalCode"},
            {title: "采购申请编码",field:"requestCode"},
            {title: "物料描述",field:"materialDesc"},
            {title: "物料编码",field:"materialCode"},
            {title: "数量",field:"purchaseCnt"},
            {title: "含税价",field:"price"},
            {title: "价格单位",field:"priceUnit"},
            {title: "订单单位",field:"poPriceUnit"},
            {title: "税代码",field:"taxCode"},
            {title: "总价",field:"totalPrice"},
            {title: "公司代码",field:"companyCode"},
            {title: "回复交期",field:"replyDeliveryDate"},
            {title: "供应商编码",field:"vendorCode"},
            {title: "供应商名称",field:"vendorName"},
            {title: "邮箱",field:"email"},
            {title: "详情",field:'',
                formatter: function(value,row,index){
                   //return '<a style="color:blue" href="#" onclick=fun("' + row.inquiryCode + '");>邮箱详情</a>';
                   return '<a class="btn btn-outline btn-success btn-rounded" data-toggle="modal" data-target="#myModal" onclick=fun("' + row.inquiryCode + '");>邮箱详情</a>';
                }
            }
        ], {pageList: [5, 10, 15, 50, 100], pageSize : 10}
    );
    //刷新数据
    function refresh() {
    	searcherBS.refresh();
    }
    initDictDataSel();//初始化所有字典数据
    /**
     * 提交
     */
    function submit() {
    	var rowDatas = searcherBS.getSelectedRows("quotationId");
    	var states = searcherBS.getSelectedRows("state");
    	
    	for(var i=0;i<states.length;i++){
    		if(states[i]==-1){
    			alert("请先填写报价单")
    			return;
    		}
    		
    	}
    	if (rowDatas.length < 1) {
	             Dialog.errorMsg("请选择一行进行提交");
	             return;
	    }
	       Page.ajaxPOST(ctxPath + "/inquiryQuotation/submit", {"quotationId" : rowDatas}, function () {
	             Dialog.msg("操作成功", function () {
	                         searcherBS.refresh();
	        });
	     });
	   
    }
 
    /**
     * 填写报价单
     */
    function inquiryQuotationadd() {
    	
        var rowDatas = searcherBS.getSelectedRows("quotationId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
		var index = Dialog.openUrl("新增报价单", ctxPath + '/inquiryQuotation/toEdit/'+rowDatas,
		    {area : ['900px','700px'],
			  maxmin: true,
		      end: function () {}
		    });
		layer.full(index)
    }
    function fun(obj){
    	 var inquiryCode =obj;
    	 /*var index = Dialog.openUrl("", ctxPath + '/parityEmail/findById/'+inquiryCode,
    			    {area : ['900px','700px'],
    				  maxmin: true,
    			      end: function () {}
    			    });
    	 layer.full(index)	*/
    	 $.ajax({
  			type: "GET",
  			url: ctxPath + '/parityEmail/findById/'+inquiryCode,
  			contentType : "application/json;charsetset=UTF-8",//必须
  			dataType:"json",//将JSONObj作为请求参数传递 ;
  			cache: true,
  			async: false,
  			error: function (request) {
  			      showTipAlert("Connection error");
  			},
  			success: function (res) {
  				var data = eval(res.result);
  				var trCode ="";
  				for(var item in data){
  				    trCode+= "<tr>";
			        trCode+= "<td style='width:5%'><input name='parityEmailInquiryCode' value='"+data[item].parityEmailInquiryCode+"' /></td>";
			        trCode+= "<td style='width:200%'><input name='parityEmail' value='"+data[item].parityEmail+"' style='width:100%' /></td>";
			        trCode+= "<td style='width:95%'><input name='data' value='"+data[item].data+"' /></td>";
			        trCode+= "</tr>";
  				  }
  				 $("#mail tbody").html("");
			     $("#mail tbody").append(trCode);
  			
  			}
  	    });
     }

    //显示发送邮件弹出框
    function show() {
        var rowData = searcherBS.getRawData();
        if (rowData.length ==0 ) {
            Dialog.errorMsg("至少选择一项发送邮件！");
            return;
        }
        var email='';
        var vendorNames ='';
        for(var index in rowData){
            if(rowData[index].email!=null&&rowData[index].email!=''){
                email+=rowData[index].email+';';
            }
            vendorNames +=rowData[index].vendorName+';';
        }
        email=email.substr(0,email.length-1);
        vendorNames=vendorNames.substr(0,vendorNames.length-1);
        $('#remark').val('');
        $('#vendorName').val(vendorNames);
        $('#email').val(email);
        Dialog.open("发送邮件", $("#myMailModal"),{area : ['800px','650px']});
        //var close=$('a.layui-layer-ico.layui-layer-close.layui-layer-close1');
        //close.mousedown(function(){$('.modal-backdrop').remove();});
    }
    //关闭模态框
    function closeDialog(){
        layer.closeAll();
    }
    function close(){
    	$("#myMailModal").modal('hide');
    }
    //发送邮件
    function sendmail(){
        //验证信息
        var reg = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        var email=$("#email").val();
        if(email==null||email.trim()==""){
            Dialog.errorMsg("邮箱不能为空！");
            return ;
        }
        email = $("#email").val().replace(/[\s]/g,"").split(';');
        for(index in email){
            if(email[index]==""||email[index]==null||typeof(email[index]) == undefined){

                email.splice(index,1);
            }else if(!reg.test(email[index].trim())){
                Dialog.errorMsg("邮箱格式不正确！");
                return ;
            }
        }
        var remark =$("#remark").val().trim();
        if(remark==null||remark==""||remark==undefined){
            Dialog.errorMsg("内容不能为空！");
            return ;
        }
        var vendorName =$("#vendorName").val().trim();
        if(vendorName==null||vendorName==""||vendorName==undefined){
            Dialog.errorMsg("供应商信息不能为空！");
            return ;
        }
        var arremail = JSON.stringify( email );

        var myMap = {};
        myMap["arremail"] = arremail;
        myMap["content"] = remark+"\n请登录网站查看：http://"+location.host+"/hssrm/sys/index";
        myMap["vendorName"] = vendorName;
        $.ajax({
            type: "POST",
            url: ctxPath + "/parityEmail/offerSendMail",
            data: JSON.stringify(myMap),//必须
            contentType : "application/json;charset=UTF-8",//必须
            cache: true,
            error: function (data) {
                Dialog.msg("发送失败，请重试！");
                closeDialog();
            },
            success: function (data) {
                if(data.result=="ok"){
                    Dialog.msg("发送成功！");
                    closeDialog();
                }else{
                    Dialog.msg("发送失败，请重试！");
                    closeDialog();
                }
            }
        });
    }
</script>
</body>
</html>